package com.kgc.sbt.scheduled;

import com.kgc.sbt.mapper.LimitBuyMapper;
import com.kgc.sbt.mapper.NoticeLimitMapper;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.SendSmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * Created on 2021/6/23.
 * <p>
 * Author: GongMingJie
 * <p>
 * Description: 抢购活动定时任务
 */
@Component
@Slf4j
public class ScheduledTask {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired(required = false)
    private NoticeLimitMapper noticeLimitMapper;

    @Autowired(required = false)
    private LimitBuyMapper limitBuyMapper;




    /**
     * Created on 2021/6/23
     * Author: GongMingJie
     *
     * @param
     * @return Description: 定时查看redis是否存在抢购活动，发送通知
     */
    @Scheduled(fixedDelay = 1 * 1000)
    public void noticebuyLimit() {
        //log.info("------ 定时任务进行中");

        List<Object> buyLimitIds = redisUtils.lGet("buyLimitIds", 0, -1);
        log.info("------ 登记抢购活动的ids为：{}", buyLimitIds);

        for (Object buyLimitId : buyLimitIds) {
            // log.info("------- 遍历的buyLimitId:{}", buyLimitId);

            // 判断此活动是否发布
            if (redisUtils.get("buyLimitIdFlag" + buyLimitId.toString()) != null){
                /**
                 * 标志：是否发过短信，0 - 未发短信
                 * 1 - 已发短信未开始
                 * 2 - 已开始未结束
                 */
                Integer flag = Integer.parseInt(redisUtils.get("buyLimitIdFlag" + buyLimitId.toString()).toString());

                // 判断redis中是否包含抢购活动
                if (!redisUtils.hasKey("buyLimitStart-60" + buyLimitId.toString()) && flag == 0) {
                    log.warn("------ 通知抢购活动{}，还有1分钟开始抢购",buyLimitId);

                    // 获取登记的用户电话信息
                    List<String> phones = noticeLimitMapper.selectByBuyLimitId((Integer) buyLimitId);
                    log.warn("------- 通知这些人抢购开始{}",phones);

                    // 通知登记的用户
                    for (String phone : phones) {
                        log.warn("------ 通知{}，还有1分钟开始抢购", phone);
                        SendSmsUtil.sendSms(phone, "123321");
                    }

                    redisUtils.set("buyLimitIdFlag" + buyLimitId.toString(), 1);
                }

                if (!redisUtils.hasKey("buyLimitStart" + buyLimitId.toString()) && flag == 1) {
                    log.warn("------ 抢购活动{}开始", buyLimitId);
                    redisUtils.set("buyLimitIdFlag" + buyLimitId.toString(), 2);
                }

                if (!redisUtils.hasKey("buyLimitEnd" + buyLimitId.toString()) && flag == 2) {

                    // 查询goodsId
                    Integer goodsId = limitBuyMapper.selectByPrimaryKey((Integer) buyLimitId).getGoodsId();
                    log.warn("------ 抢购{}结束，商品的id={}", buyLimitId,goodsId);
                    // TODO 减库存  limitBuyMapper.decStock(goodsId,redisUtils.get("prod-stock-num-" + goodsId));

                    redisUtils.del("buyLimitIdFlag" + buyLimitId.toString());
                    redisUtils.lRemove("buyLimitIds", 0, buyLimitId);
                }
            }
        }
    }
}
